/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.component.internal;

import org.springframework.beans.factory.InitializingBean;

import com.elancom.framework.component.Component;
import com.elancom.framework.component.ComponentException;
import com.elancom.framework.component.ComponentInvoker;
import com.elancom.framework.component.ComponentRepository;
import com.elancom.framework.component.Input;
import com.elancom.framework.component.InputFactory;
import com.elancom.framework.component.Output;

/**
 * ELANCOM TODO : Input/Output的创建待重构,请抽象
 * 
 * @author elancom
 * @date 2012-7-12
 */
public class ComponentInvokerImpl implements InitializingBean, ComponentInvoker {
	private ComponentRepository componentRepository;

	private InputFactory inputFactory;

	@Override
	public void afterPropertiesSet() throws Exception {
		if (componentRepository == null) {
			throw new ComponentException("请初始化 componentRepository.");
		}

		if (inputFactory == null) {
			throw new ComponentException("请初始化 inputFactory.");
		}
	}

	/**
	 * @param componentName
	 * @return
	 */
	private Component getComponent(String componentName) {
		Component component = null;

		try {
			component = componentRepository.getComponent(componentName);
		} catch (Exception e) {
			e.printStackTrace(); // TODO
		}

		if (component == null) {
			throw new RuntimeException("找不到组件");
		}

		return component;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.ComponentInvoker#execute(com.elancom.framework.component.Component)
	 */
	@Override
	public Output execute(Component component) throws ComponentException {
		Input input = inputFactory.create();
		return execute(component, input);
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.ComponentInvoker#execute(com.elancom.framework.component.Component, com.elancom.framework.component.Input)
	 */
	@Override
	public Output execute(Component component, Input input) throws ComponentException {
		Output output = component.execute(input);
		// TODO output
		return output;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.ComponentInvoker#execute(java.lang.String)
	 */
	@Override
	public Output execute(String componentName) throws ComponentException {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.ComponentInvoker#execute(java.lang.String, com.elancom.framework.component.Input)
	 */
	@Override
	public Output execute(String componentName, Input input) throws ComponentException {
		return execute(getComponent(componentName));
	}

	/**
	 * @param componentRepository
	 *            the componentRepository to set
	 */
	public void setComponentRepository(ComponentRepository componentRepository) {
		this.componentRepository = componentRepository;
	}

	/**
	 * @return the inputFactory
	 */
	public InputFactory getInputFactory() {
		return inputFactory;
	}

	/**
	 * @param inputFactory
	 *            the inputFactory to set
	 */
	public void setInputFactory(InputFactory inputFactory) {
		this.inputFactory = inputFactory;
	}
}
